﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace VirtualPress.Infrastructure.Entities.Repository
{
    [Table("T_ArticleHistory")]
    public class T_ArticleHistory : IEntity<T_ArticleHistory>
    {
        [Key]
        [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        public int ManuscriptId { get; set; }

        /// <summary>
        /// 上一状态
        /// </summary>
        public int PreStatus { get; set; }

        /// <summary>
        /// 当前状态
        /// </summary>
        public int CurrentStatus { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(256)]
        public string Message { get; set; }

        /// <summary>
        /// 支付交易号
        /// </summary>
        [Column(TypeName = "varchar")]
        [StringLength(128)]
        public string Txid { get; set; }

        [Column(TypeName="datetime")]
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 操作员
        /// </summary>
        [Column(TypeName="nvarchar")]
        [StringLength(64)]
        public string Operator { get; set; }

        public override T_ArticleHistory Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.Id == id);
        }

        public override T_ArticleHistory Find(System.Linq.Expressions.Expression<Func<T_ArticleHistory, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.ArticleHistory.FirstOrDefault(where);
                if (item != null)
                {
                    this.CreateTime = item.CreateTime;
                    this.CurrentStatus = item.CurrentStatus;
                    this.Id = item.Id;
                    this.ManuscriptId = item.ManuscriptId;
                    this.Message = item.Message;
                    this.Operator = item.Operator;
                    this.PreStatus = item.PreStatus;
                }
                return this;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_ArticleHistory, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.ArticleHistory.Any(where);
            }
        }
    }
}
